﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace wifi_test
{
    class cls_record
    {
        IPEndPoint _cmdPoint;   //用于保存命令的接口
        IPEndPoint _datPoint;   //用于保存指令回应的数据端口
        uint _dstRecCount;       //目标设备接收到包的个数
        uint _dstSendCount;      //目标设备发送包的个数
        uint _dstSystemRecCount; //目标设备系统接收到包的个数
        uint _dstSystemSendCount;//目标设备系统发送包的个数 
        MainForm MF;

        /// <summary>
        /// 以下是属性区域
        /// </summary>
        #region 属性区域

        public IPEndPoint cmdPoint
        {
            get
            {
                return _cmdPoint;
            }
        }

        public IPEndPoint datPoint
        {
            get
            {
                return _datPoint;
            }
            set
            {
                _datPoint = value;
            }
        }

        public uint dstRecCount
        {
            get
            {
                return _dstRecCount;
            }
            set
            {
                _dstRecCount = value;
            }
        }

        public uint dstSendCount
        {
            get
            {
                return _dstSendCount;
            }
            set
            {
                _dstSendCount = value;
            }
        }

        public uint dstSystemRecCount
        {
            get
            {
                return _dstSystemRecCount;
            }
            set
            {
                _dstSystemRecCount = value;
            }
        }


        public uint dstSystemSendCount
        {
            get
            {
                return _dstSystemSendCount;
            }
            set
            {
                _dstSystemSendCount = value;
            }
        }

        #endregion

        /*public struct record
        {
            public MainForm.recordstate state;
            public string time;
            public string data;

            public record(MainForm.recordstate param_state, string param_time, string param_data)
            {
                this.state = param_state;
                this.time = param_time;
                this.data = param_data;
            }
        };*/

        //private int record_count = 0;
        //private int recordspace = 10;

        //record [] record_sp = new record[10];


        /// <summary>
        /// 设置目标机的指令接口
        /// </summary>
        /// <param name="destValue">目标机的指令接口</param>
        public void sub_setCmdPoint(IPEndPoint destValue)
        {
            _cmdPoint = new IPEndPoint(destValue.Address, destValue.Port);
        }


        /// <summary>
        /// 设置主窗口的关系
        /// </summary>
        /// <param name="mainForm">主窗口</param>
        /// 作者:xhd
        /// 建立时间:09.10.20
        public void sub_setMF(MainForm mainForm)
        {
            MF = mainForm;
        }


        /// <summary>
        /// 把要记录的数据转成struct record格式，并加入到record_sp中去
        /// </summary>
        /// <param name="state">要记录的原因</param>
        /// <param name="data">记录的内容</param>
        /*public void sub_recordin(MainForm.recordstate state, string data)
        {
            string xustr = System.DateTime.Now.ToString();
            if ((record_count + 1) > recordspace)
            {
                Array.Resize(ref record_sp, record_sp.Length + 10);
                recordspace += 10;
            }

            record tmp_record = new record(state, xustr, data);

            record_sp.SetValue(tmp_record, record_count);
            record_count += 1;
            if (record_count > 100)
            {
                sub_genrecordfile();
            }
        }
        */


        /// <summary>
        /// 把相关的信息记录到文件中去同时清空record数组
        /// </summary>
        /*public void sub_genrecordfile()
        {
            if (record_count != 0)
            {
                string _FilePath = "ErrLog.txt";
                System.IO.FileStream oFileStream;
                if (!System.IO.File.Exists(_FilePath))
                {
                    oFileStream = new System.IO.FileStream(_FilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
                }
                else
                {
                    oFileStream = new System.IO.FileStream(_FilePath, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
                }
                System.IO.StreamWriter oStreamWriter = new System.IO.StreamWriter(oFileStream, System.Text.Encoding.Default);

                string _FileContent = null;
                record tmp_record;

                int i;
                for (i = 0; i < record_count; i++)
                {
                    tmp_record = (record)record_sp.GetValue(i);
                    switch (tmp_record.state)
                    {
                        case MainForm.recordstate.endtest:
                            _FileContent = "结束测试：" + tmp_record.time + " " + tmp_record.data;
                            break;

                        case MainForm.recordstate.recerror:
                            _FileContent = "接收出错：" + tmp_record.time + " " + tmp_record.data;
                            break;

                        case MainForm.recordstate.senderror:
                            _FileContent = "发送出错：" + tmp_record.time + " " + tmp_record.data;
                            break;

                        case MainForm.recordstate.starttest:
                            _FileContent = "开始测试：" + tmp_record.time + " ";
                            break;
                    }
                    oStreamWriter.Write(_FileContent);
                    oStreamWriter.WriteLine();
                }

                record_sp = null;

                record_count = 0;
                recordspace = 10;

                record_sp = new record[10];

                oStreamWriter.Close();
                oFileStream.Close();
            }
        }*/


        /// <summary>
        /// 从指令端口获取目标机器的信息
        /// </summary>
        /// <returns>返回-1表示处理失败了</returns>
        /// 作者:xhd
        /// 建立日期:09.10.20
        public int fun_GetDestInfo()
        {
            byte[] xsendbuf;

            xsendbuf = new byte[] { 99, 111, 109, 109, 97, 110, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            MF.sub_sendData(xsendbuf, _cmdPoint);

            byte[] sendbuf;
            sendbuf = MF.fun_recTmpData(datPoint);
            if (sendbuf != null)
            {
                //已经取到了数据了
                uint tmp1;

                tmp1 = sendbuf[0];
                tmp1 = (tmp1 << 8) | sendbuf[1];
                tmp1 = (tmp1 << 8) | sendbuf[2];
                tmp1 = (tmp1 << 8) | sendbuf[3];

                _dstSystemSendCount = tmp1;

                tmp1 = sendbuf[4];
                tmp1 = (tmp1 << 8) | sendbuf[5];
                tmp1 = (tmp1 << 8) | sendbuf[6];
                tmp1 = (tmp1 << 8) | sendbuf[7];

                _dstSystemRecCount = tmp1;

                //越过错的数据

                tmp1 = sendbuf[12];
                tmp1 = (tmp1 << 8) | sendbuf[13];
                tmp1 = (tmp1 << 8) | sendbuf[14];
                tmp1 = (tmp1 << 8) | sendbuf[15];

                _dstRecCount = tmp1;

                tmp1 = sendbuf[16];
                tmp1 = (tmp1 << 8) | sendbuf[17];
                tmp1 = (tmp1 << 8) | sendbuf[18];
                tmp1 = (tmp1 << 8) | sendbuf[19];

                _dstSendCount = tmp1;

                string xustr = "设备系统接收：" + _dstSystemRecCount.ToString() + " 设备系统发送：" + _dstSystemSendCount.ToString() + "\n";
                MF.xulog(MainForm.LogMsgType.Incoming, xustr);

                xustr = "设备接收：" + _dstRecCount + " 设备发送：" + _dstSendCount.ToString() + "\n";
                MF.xulog(MainForm.LogMsgType.Incoming, xustr);

                MF.sub_closeTmpUdpClient();

                return 1;
            }
            else
            {
                string xustr = "与目标设备指令端口出错！！！\n";
                MF.xulog(MainForm.LogMsgType.Error, xustr);
                return -1;
            }
        }
        
    }
}
